<?php /* УТФ-8:fix */

class SPIcontroller__logos__manage extends SPIcontroller__logos
{
    public function gen_menu()
    {
        $menuitems = array();

        $in_topic = SPI::I('var')->get('topic');

        $menuitems['MAIN'][] = array(
            'access'        => array( SPI_IAM_PERM_ADMIN ),
            'link'          => $this->pack_link(),
            'icon'          => 'logos__manage.gif',
            'name'          => SPI::I('lang')->logos->menu,
            'is_active'     => $in_topic == $this->CONTROLLER_ID
        );

        return $menuitems;
    }

    public function pack_link( $vars = null )
    {
        return SPIc::I('manage')->link->pack( $this->CONTROLLER_ID, null, $vars );
    }

    public function exec( & $content )
    {
        if( SPI::I('Iam')->is_permit( SPI_IAM_PERM_ADMIN ) === true )
        {
            # access allow
        }
        else return false;

        $data = array();

        $data['topic']  = SPI::I('var')->get('topic');
        $data['action'] = SPI::I('var')->get('action');

        switch( $data['action'] )
        {
            default:
            {
                $data['action'] = 'view';
            }
            case 'view':
            {
                if( $this->exec_view( $content, $data ) === false )
                {
                    return false;
                }
                break;
            }

            case 'insert':
            case 'modify':
            {
                $this->exec_savedata( $content, $data ); # => die by json-data response
            }

            case 'form':
            {
                if( $this->exec_form( $content, $data ) === false )
                {
                    return false;
                }
                break;
            }
        }

        $content['data'] = $data;

        return true;
    }

    private function exec_view( & $content, & $data, $is_recursive_exec = false )
    {
        if( false === ( $mLogos = SPIm::I('logos') ) )
        {
            return false;
        }

        $data['title']    = SPI::I('lang')->logos->title;
        $data['subtitle'] = SPI::I('lang')->manage->viewmode_list;
        $data['template'] = 'logos/view.tpl';

        $data['link_newitem'] = $this->pack_link( array( 'action'=>'form' ) );

        // --------------------------------------------------------------------

        $action_do = SPI::I('var')->get('action_do' );

        if( $is_recursive_exec === false) // if $is_recursive_exec == true --> don't need to ReExecute action
        if( in_array( $action_do, array( 'activity_on', 'activity_off', 'delete' ) ) === true )
        {
            $id_list = SPI::I('var')->get( 'multiselect' );
            
            if( is_array( $id_list ) === false )
            {
                $id_list[] = SPI::I('var')->get( 'iid' );
            }

            foreach( $id_list as $item_id )
            {
                $item_id = (int) $item_id;

                if( $item_id == 0 )
                {
                    continue;
                }

                switch( $action_do )
                {
                    case 'activity_on':  $mLogos->manage->set_activity( 'on', $item_id ); break;
                    case 'activity_off': $mLogos->manage->set_activity( 'off', $item_id ); break;
                    case 'delete':
                    {
                        SPIc::I('media')->manage->delete_group( $this->MEDIA_GID, $item_id );
                        $mLogos->manage->delete( $item_id );
                        break;
                    }
                }
            }
            unset( $id_list, $item_id );

            SPIc::I('manage')->notifier->set_message( $content, true, SPI::I('lang')->notifier->msg_operation_complite );
        }

        // --------------------------------------------------------------------
        // Make notifications by 'action_complite' value

        switch( SPI::I('var')->get('action_complite') )
        {
            case 'insert': SPIc::I('manage')->notifier->set_message( $content, true, SPI::I('lang')->notifier->msg_insert_complite ); break;
            case 'modify': SPIc::I('manage')->notifier->set_message( $content, true, SPI::I('lang')->notifier->msg_modify_complite ); break;
        }

        // --------------------------------------------------------------------
        // Filter objects [stdobj]

        $stdobj_baseurl = $this->pack_link( array( 'action'=>'view' ) );

        $content['stdobj']['f_search'] = SPIc::I('manage')->stdobj->filter(
            $data['topic'],
            "search",
            $stdobj_baseurl
        );

        // --------------------------------------------------------------------
        // Order fields [stdobj]

        $content['stdobj']['order'] = SPIc::I('manage')->stdobj->order(
            $data['topic'],
            $stdobj_baseurl,
            array( 'id-', 'name+' )
        );

        // --------------------------------------------------------------------

        $qresult = $mLogos->manage->select(
            $content['stdobj']['f_search']['value'],
            $content['stdobj']['order']['current']
        );

        if( $qresult === false )
        {
            return false;
        }
        
        $data['items_count'] = $qresult->num_rows();

        // --------------------------------------------------------------------
        // Pager [stdobj]

        $content['stdobj']['pager'] = SPIc::I('manage')->stdobj->pager(
            $data['topic'],
            $stdobj_baseurl,
            $data['items_count']
        );

        // --------------------------------------------------------------------
        // Generate List

        $qresult->seek( $content['stdobj']['pager']['firstitem_index'] );
        $qresult->set_manual_limit( $content['stdobj']['pager']['items_per_page'] );

        $data['items'] = array();

        while( $item = $qresult->fetch() )
        {
            $item['preview'] = SPIm::I('media')->get_public_sourceByIndexOld(
                $this->MEDIA_GID, $item['id'], 1,
                array( 'mode' => 'preview' )
            );

            if( $item['workstatus'] == '0' )
            {
                $item['link_activity_on'] = $this->pack_link( array( 'action'=>'view', 'action_do'=>'activity_on', 'iid'=>$item['id'] ) );
            }
            elseif( $item['workstatus'] == '1' ) 
            {
                $item['link_activity_off'] = $this->pack_link( array( 'action'=>'view', 'action_do'=>'activity_off', 'iid'=>$item['id'] ) );
            }

            $item['link_modify'] = $this->pack_link( array( 'action'=>'form', 'iid'=>$item['id'] ) );
            $item['link_delete'] = $this->pack_link( array( 'action'=>'view', 'action_do'=>'delete', 'iid'=>$item['id'] ) );

            $data['items'][] = $item;
        }
        $qresult->close();

        if( count( $data['items'] ) == 0 && $data['items_count'] != 0 && $is_recursive_exec === false )
        {
            // Reset page index to number '1'
            SPIc::I('manage')->stdobj->pager_reset( $data['topic'] );

            // Recursive run
            return $this->exec_view( $content, $data, true );
        }

        // --------------------------------------------------------------------

        $data['formvars'] = array();
        $data['formvars']['action'] = $data['action'];

        $data['link_form_submit'] = $this->pack_link();

        // --------------------------------------------------------------------

        return true;
    }

    private function exec_form( & $content, & $data )
    {
        if( false === ( $mLogos = SPIm::I('logos') ) )
        {
            return false;
        }

        $data['title']    = SPI::I('lang')->logos->title;
        $data['subtitle'] = '';
        $data['template'] = 'logos/form.tpl';

        $data['link_back'] = $this->pack_link();

        $item_id = (int) SPI::I('var')->get( 'iid' );

        if( $item_id )
        {
            $data['formmode'] = 'modify';
            $data['subtitle'] = SPI::I('lang')->manage->viewmode_form_update;

            $item_fields = $mLogos->manage->get_by_id( $item_id );
        }
        else
        {
            $data['formmode'] = 'insert';
            $data['subtitle'] = SPI::I('lang')->manage->viewmode_form_newitem;

            $item_fields = array();
        }

        $content['item_fields'] = $item_fields;

        // --------------------------------------------------------------------

        $cMedia = SPIc::I('media');

        $content['media'] = $cMedia->manage->initialize(
            $this->MEDIA_GID,
            $item_id ? $item_id : null,
            array( $cMedia->PRESET_IMAGES ),
            array( 'limit' => 1 )
        );

        // --------------------------------------------------------------------

        $data['formvars'] = array();
        $data['formvars']['action'] = $data['formmode'];

        if( $item_id )
        {
            $data['formvars']['iid'] = $item_id;
        }
        else
        {
            $data['formvars']['media_gid'] = $content['media']['gid'];
        }

        $data['link_form_submit'] = $this->pack_link();

        // --------------------------------------------------------------------

        return true;
    }

    private function exec_savedata( & $content, & $data )
    {
        if( false === ( $mLogos = SPIm::I('logos') ) )
        {
            return false;
        }

        if( in_array( $data['action'], array( 'insert', 'modify' ) ) === false )
        {
            trigger_error( 'Undefined action ('.$data['action'].')', E_USER_ERROR );
        }

        // --------------------------------------------------------------------

        if( $data['action'] == 'insert' )
        {
            $item_id = null;
        }
        else # modify
        {
            $item_id = (int) SPI::I('var')->get('iid');
        }

        $in_values = SPI::I('var')->get('field');

        // --------------------------------------------------------------------

        $validator = SPIc::I('validator')->open();

        $validator->add_rule( 'workstatus',  'integer[0:1]' );
        $validator->add_rule( 'name',        'string[1:255]' );

        list( $status, $validator_data ) = $validator->exec( $in_values );


        if( $status === false )
        {
            $response = array();
            $response['status'] = 'error';
            $response['error_messages'] = $validator_data; // as errors
            SPIv::I('json')->render($response);
            die();
        }

        // --------------------------------------------------------------------
        // Pre insert | modify actions

        $in_values = $validator_data; // as parsed values

        // --------------------------------------------------------------------
        // Do insert | modify

        if( $data['action'] == 'insert' )
        {
            $status = $mLogos->manage->insert( $in_values );

            if( $status !== false )
            {
                $item_id = $status;
                $status = true;
            }
        }
        else # modify
        {
            $status = $mLogos->manage->modify( $item_id, $in_values );
        }

        // --------------------------------------------------------------------
        // Post insert | modify actions

        if( $status === true && $data['action'] == 'insert' )
        {
            SPIc::I('media')->manage->attach_pack_for_newitem(
                SPI::I('var')->get('media_gid'),
                $item_id
            );
        }

        // --------------------------------------------------------------------

        $response = array();
        $response['status'] = 'complite';
        $response['link_redirect'] = $this->pack_link( array( 'action'=>'view', 'action_complite' => $data['action'] ) );
        SPIv::I('json')->render($response);
        die();
    }
}
